<template>
  <div>
    <span>&nbsp;</span>
    <el-form :inline="true" :model="loanQuery" class="demo-form-inline" style="border-radius: 4px">
      <span>&nbsp;</span>
      <span>&nbsp;</span>
      <el-form-item label="对象">
        <el-input v-model="loanQuery.target" placeholder="对象" style="width:160px"></el-input>
      </el-form-item>
      <el-form-item label="金额">
        <el-input v-model="loanQuery.num" placeholder="金额"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="loanQuery.kind" placeholder="类型">
          <el-option :value="1" label="借入"></el-option>
          <el-option :value="2" label="借出"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="借款时间">
        <el-col :span="18">
          <el-date-picker v-model="loanQuery.start_time" type="date" placeholder="选择日期"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="还款时间">
        <el-col :span="18">
          <el-date-picker v-model="loanQuery.final_time" type="date" placeholder="选择日期"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getpageList()">查询</el-button>
        <el-button type="success" @click="empty">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="loanList" border style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column label="序号" width="100" align="center">
        <template slot-scope="scope">{{ (page - 1) * limit + scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column prop="kind" label="类型" align="center" width="220">
         <template slot-scope="scope">{{ scope.row.kind==='1'?'借入':'借出' }}</template>
      </el-table-column>
      <el-table-column prop="target" label="对象" align="center" width="220"></el-table-column>
      <el-table-column prop="num" label="金额" align="center"></el-table-column>
      <el-table-column prop="startTime" label="借款时间" align="center"></el-table-column>
      <el-table-column prop="finalTime" label="还款时间" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-popover placement="bottom" width="200" trigger="click">
            {{description}}
            <el-button
              type="success"
              size="mini"
              round
              slot="reference"
              @click="getdescription(scope.row.id)"
            >查看备注</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <router-link :to="'/jd/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit" round>修改</el-button>
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removedetailed(scope.row.id)"
            round
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getpageList"
    />
  </div>
</template>

<style>
.el-table .pay {
  background: #f5f5f500;
}

.el-table .income {
  background: #fbfcfb00;
}
</style>

<script>
import loanApi from "@/api/jd/list";

export default {
  data() {
    return {
      loanList: [],
      loanQuery: {},
      page: 1, //当前页数
      total: 0, //总记录数
      limit: 10 ,//每页记录数
      description:""
    };
  },
  created() {
    this.getpageList();
  },
  methods: {
    getdescription(id){
      loanApi.getDescription(id).then(response=>{
        this.description=response.data.description
      })
    },
    empty() {
      this.loanQuery = {};
      this.getpageList()
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.type === "2") {
        return "pay";
      } else {
        return "income";
      }
    },
    getpageList(page = 1) {
      this.page = page;
      loanApi
        .getLoanList(this.page, this.limit, this.loanQuery)
        .then(response => {
          this.total = response.data.total;
          this.loanList = response.data.item;
          console.log(this.loanList)
        });
    },
    removedetailed(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        //点击确定，执行删除方法
        //执行删除方法
        loanApi.delete(id).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          }),
            //回到列表页面，刷新
            this.getpageList();
        });
      });
    }
  }
};
</script>